/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bikes.ship;

import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Aganguly
 */
@WebService
@Stateless()
public class Shipper {

    @PersistenceContext(unitName = "FirstClassShippingPU")
    EntityManager em;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "createNewShipment")
    public long createNewShipment(@WebParam(name = "shipmentDetails") String shipmentDetails, @WebParam(name = "shippingDate") Date shippingDate) {
        long result = -1;
        try {
            Shipmentrecord record = new Shipmentrecord();
            record.setShipmentDetails(shipmentDetails);
            record.setShipDate(shippingDate);
            record.setStatus(ShipmentStatus.NotDelivered.toString());
            em.persist(record);
            result = record.getShipmentId();
        } catch (Exception ex) {
        }
        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "setShipmentStatusShipped")
    public Boolean setShipmentStatusShipped(@WebParam(name = "shipmentId") long shipmentId, @WebParam(name = "status") String status) {
        return setStatus(shipmentId, ShipmentStatus.InTransit);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "setShipmentStatusDelivered")
    public Boolean setShipmentStatusDelivered(@WebParam(name = "shipmentId") long shipmentId, @WebParam(name = "status") String status) {
        boolean result = false;
        result = setStatus(shipmentId, ShipmentStatus.Delivered);
        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "setShipmentStatusNotDelivered")
    public Boolean setShipmentStatusNotDelivered(@WebParam(name = "shipmentId") long shipmentId, @WebParam(name = "status") String status) {
        return setStatus(shipmentId, ShipmentStatus.NotDelivered);
    }

    private boolean setStatus(long shipmentId, ShipmentStatus status) {
        boolean result = false;
        javax.persistence.Query q = em.createQuery("SELECT s FROM Shipmentrecord s WHERE s.shipmentId = :shipmentId");
        q.setParameter("shipmentId", shipmentId);
        List<Shipmentrecord> resultList = (List<Shipmentrecord>) q.getResultList();
        if (resultList != null && resultList.size() > 0) {
            Shipmentrecord record = resultList.get(0);
            record.setStatus(status.toString());
            if (status.equals(ShipmentStatus.Delivered)) {
                record.setDeliveryDate(new Date());
            }
            result = true;
        }
        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getShipmentStatus")
    public String getShipmentStatus(@WebParam(name = "shipmentId") long shipmentId) {
        String result = "";
        javax.persistence.Query q = em.createQuery("SELECT s FROM Shipmentrecord s WHERE s.shipmentId = :shipmentId");
        q.setParameter("shipmentId", shipmentId);
        List<Shipmentrecord> resultList = (List<Shipmentrecord>) q.getResultList();
        if (resultList != null && resultList.size() > 0) {
            Shipmentrecord record = resultList.get(0);
            result = record.getStatus();
        }
        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getShipperDetails")
    public String getShipperDetails(@WebParam(name = "shipmentId") long shipmentId) {
        return "First-Class Shippers, 300 S Craig St., Pittsburgh, PA. Tel: 412-333-4343";
    }
}
